package com.chagee.store.operation.server.controller;

import com.alibaba.fastjson.JSON;
import com.chagee.base.utils.*;
import com.chagee.store.operation.api.request.AcceptanceOrderRequest;
import com.chagee.store.operation.api.request.AcceptanceRequest;
import com.chagee.store.operation.common.exception.BizErrorCode;
import com.chagee.store.operation.common.utils.CommonUtil;
import com.chagee.store.operation.server.facade.StoreAppSweepFacade;
import com.chagee.store.operation.server.model.param.*;
import com.chagee.store.operation.server.model.vo.ScanResponse;
import com.chagee.store.operation.server.model.vo.StoreMonthOrderVO;
import com.chagee.store.operation.server.model.vo.StoreOderInfoVO;
import com.chagee.store.operation.server.model.vo.QueryStoreTaskResponse;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springblade.core.secure.utils.AuthUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

/**
 * APP端三方打烊接口
 *
 * @Author : hk
 * @Date : 2024/5/10
 */

@Slf4j
@RestController
@RequestMapping("/appSweep")
public class StoreAppSweepController {

	@Resource
	private StoreAppSweepFacade storeAppSweepFacade;

	@PostMapping("/list")
	@ApiOperation("打烊任务列表")
	public HttpResponse<PaginationData<QueryStoreTaskResponse>> list(@RequestBody @Valid PaginationRequest<StoreAppSweepQueryParam> request) {
		log.info("store list request:{}, auth:{}", JSON.toJSONString(request), JSON.toJSONString(AuthUtil.getUser()));
		if (StringUtils.isNotBlank(CommonUtil.validate(request.getQueryParameter()))) {
			return HttpResponseUtil.build(null, BizErrorCode.REQUEST_VALID_FAILED.getErrorCode(), CommonUtil.validate(request.getQueryParameter()));
		}
		if (Objects.isNull(request.getPageNum()) || Objects.equals(0, request.getPageNum())) {
			request.setPageNum(1);
		}
		if (Objects.isNull(request.getPageSize()) || Objects.equals(0, request.getPageSize())) {
			request.setPageSize(10);
		}
		PaginationData<QueryStoreTaskResponse> list = storeAppSweepFacade.queryList(request);
		return HttpResponseUtil.success(list);
	}

	@PostMapping("/orderTask")
	@ApiOperation("任务列表")
	public HttpResponse<PaginationData<QueryStoreTaskResponse>> queryOrderTaskList(@RequestBody @Valid PaginationRequest<OrderTaskListParam> request) {
		log.info("StoreAppSweepController_queryOrderTaskList_request:{}, auth:{}", JSON.toJSONString(request), JSON.toJSONString(AuthUtil.getUser()));
		if (StringUtils.isNotBlank(CommonUtil.validate(request.getQueryParameter()))) {
			return HttpResponseUtil.build(null, BizErrorCode.REQUEST_VALID_FAILED.getErrorCode(), CommonUtil.validate(request.getQueryParameter()));
		}
		PaginationData<QueryStoreTaskResponse> list = storeAppSweepFacade.queryOrderTaskList(request);
		return HttpResponseUtil.success(list);
	}

	/**
	 * 门店本月单据状态详情
	 *
	 * @param request
	 * @return
	 */
	@PostMapping("/storeMonthOrder")
	@ApiOperation("门店本月单据状态详情")
	public HttpResponse<StoreMonthOrderVO> storeMonthOrder(@RequestBody @Valid MonthOrderParam request) {
		log.info("store_storeMonthOrder_request:{}, auth:{}", JSON.toJSONString(request), JSON.toJSONString(AuthUtil.getUser()));
		if (StringUtils.isNotBlank(CommonUtil.validate(request))) {
			return HttpResponseUtil.build(null, BizErrorCode.REQUEST_VALID_FAILED.getErrorCode(), CommonUtil.validate(request.getStoreNo()));
		}
		StoreMonthOrderVO storeOderVO = storeAppSweepFacade.storeMonthOrder(request);
		return HttpResponseUtil.success(storeOderVO);
	}

	/**
	 * 门店最近打烊工单日期
	 *
	 * @param request
	 * @return
	 */
	@PostMapping("/queryOrderDate")
	@ApiOperation("门店最近打烊工单日期")
	public HttpResponse<String> queryOrderDate(@RequestBody @Valid StoreWordInfoParam request) {
		log.info("store queryStoreDate request:{}, auth:{}", JSON.toJSONString(request), JSON.toJSONString(AuthUtil.getUser()));
		if (StringUtils.isNotBlank(CommonUtil.validate(request))) {
			return HttpResponseUtil.build(null, BizErrorCode.REQUEST_VALID_FAILED.getErrorCode(), CommonUtil.validate(request));
		}
		String orderDate = storeAppSweepFacade.queryOrderDate(request);
		return HttpResponseUtil.success(StringUtils.isNotBlank(orderDate) ? orderDate : "");
	}

	/**
	 * 打烊任务工单
	 *
	 * @return
	 * @paramrequest
	 * @return
	 */
	@PostMapping("/storeOrder")
	@ApiOperation("打烊任务工单")
	public HttpResponse<List<StoreOderInfoVO>> storeOrder(@RequestBody @Valid PaginationRequest<StoreAppSweepQueryParam> request) {
		log.info("store_storeOrder_request:{}, auth:{}", JSON.toJSONString(request), JSON.toJSONString(AuthUtil.getUser()));
		if (StringUtils.isBlank(request.getQueryParameter().getStoreNo()) || StringUtils.isBlank(request.getQueryParameter().getCheckDate())) {
			return HttpResponseUtil.build(null, BizErrorCode.REQUEST_VALID_FAILED.getErrorCode(), BizErrorCode.REQUEST_VALID_FAILED.getErrorMsg());
		}
		List<StoreOderInfoVO> storeOderVO = storeAppSweepFacade.storeOrder(request);
		return HttpResponseUtil.success(storeOderVO);
	}

	@PostMapping("/queryOrderDetail")
	@ApiOperation("打烊工单")
	public HttpResponse<StoreOderInfoVO> queryOrderDetail(@RequestBody @Valid QueryOrderDetailParam param) {
		log.info("StoreAppSweepController_queryOrderDetail request:{}, auth:{}", JSON.toJSONString(param), JSON.toJSONString(AuthUtil.getUser()));
		if (StringUtils.isNotBlank(CommonUtil.validate(param))) {
			return HttpResponseUtil.build(null, BizErrorCode.REQUEST_VALID_FAILED.getErrorCode(), CommonUtil.validate(param));
		}
		StoreOderInfoVO storeOderVO = storeAppSweepFacade.queryOrderDetail(param);
		return HttpResponseUtil.success(storeOderVO);
	}

	/**
	 * 任务详情
	 * @param request
	 * @return
	 */
	@PostMapping("/taskDetail")
	@ApiOperation("打烊任务详情")
	public HttpResponse<QueryStoreTaskResponse> taskDetail(@RequestBody @Valid StoreTaskInfoRequest request) {
		log.info("store taskDetail request:{}, auth:{}", JSON.toJSONString(request), JSON.toJSONString(AuthUtil.getUser()));
		if (StringUtils.isNotBlank(CommonUtil.validate(request))) {
			return HttpResponseUtil.build(null, BizErrorCode.REQUEST_VALID_FAILED.getErrorCode(), CommonUtil.validate(request));
		}
		QueryStoreTaskResponse response = storeAppSweepFacade.taskDetail(request);
		return HttpResponseUtil.success(response);
	}

	@PostMapping("/taskAcceptance")
	@ApiOperation("门店任务验收/修改")
	public HttpResponse<ScanResponse> taskAcceptance(@RequestBody @Valid AcceptanceRequest taskAcceptanceParam) {
		log.info("store_taskAcceptance_request:{}, auth:{}", JSON.toJSONString(taskAcceptanceParam), JSON.toJSONString(AuthUtil.getUser()));
		HttpResponse<ScanResponse> response = storeAppSweepFacade.taskAcceptance(taskAcceptanceParam);
		response.setErrcode(response.getData().getResponseCode());
		response.setErrmsg(response.getData().getResponseMessage());
		response.setData(null);
		return response;
	}

	@PostMapping("/orderAcceptance")
	@ApiOperation("工单验收")
	public HttpResponse<Boolean> orderAcceptance(@RequestBody @Valid AcceptanceOrderRequest acceptanceOrderParam) {
		log.info("orderAcceptance_request:{}, auth:{}", JSON.toJSONString(acceptanceOrderParam), JSON.toJSONString(AuthUtil.getUser()));
		if (StringUtils.isNotBlank(CommonUtil.validate(acceptanceOrderParam))) {
			return HttpResponseUtil.build(null, BizErrorCode.REQUEST_VALID_FAILED.getErrorCode(), CommonUtil.validate(acceptanceOrderParam));
		}
		return HttpResponseUtil.success(storeAppSweepFacade.orderAcceptance(acceptanceOrderParam));
	}
}
